<template>
  <div class="article-container">
     <!-- 导航栏 -->
    <van-nav-bar
      fixed
      left-arrow
      @click-left="$router.back()"
      title="文章详情"
    ></van-nav-bar>
    <!-- 文章详情 -->
    <div class="loading" v-if="isLoading">
      <van-loading class="inner" type="spinner" color="#1989fa" />
    </div>
    <div class="detail" v-else>
      <h3 class="title">{{detail.title}}</h3>
      <div class="author">
        <van-image round width="1rem" height="1rem" fit="fill" :src="detail.aut_photo" />
        <div class="text">
          <p class="name">{{detail.aut_name}}</p>
          <p class="time">{{detail.pubdate | data}}</p>
        </div>
        <van-button
          round
          size="small"
          type="info"
          @click="follow(detail.aut_id)"
        >{{detail.is_followed? '取关': '+ 关注'}}</van-button>
      </div>
      <div class="content">
        <div v-html="detail.content"></div>
      </div>

      <van-divider>END</van-divider>

      <div class="zan">
        <van-button
         round
         size="small"
         hairline
         type="primary"
         plain
         @click="like(detail.art_id)"
         :icon="detail.attitude === 1? 'good-job':'good-job-o'">
          {{detail.attitude === 1? '取消点赞':'点赞'}}
        </van-button>
        &nbsp;&nbsp;&nbsp;&nbsp;
        <van-button
          round
          size="small"
          hairline
          type="danger"
          plain
          icon="delete"
          @click="dislike(detail.art_id)"
        >
          {{detail.attitude === 0? '取消': '不喜欢'}}
        </van-button>
      </div>
    </div>
    <Comment></Comment>
  </div>
</template>

<script>
import Comment from './comment.vue'
import { addDislike, escZan, getArticleDetail, goArticleZan, unDislike } from '@/api/article'
import { goFollow, unFollow } from '@/api/user'
// import { goFollow } from '@/api/user'
export default {
  name: 'Detail',
  data () {
    return {
      isLoading: true,
      detail: {}
    }
  },
  async created () {
    const res = await getArticleDetail(this.$route.params.id)
    this.detail = res.data
    this.isLoading = false
  },
  methods: {
    async follow (id) {
      if (!this.$store.state.user.token.token) {
        this.$router.push({
          path: '/login',
          query: {
            goBack: 'aa'
          }
        })
        this.$toast('请先登录')
        return
      }
      if (this.detail.is_followed) {
        await goFollow(id)
      } else {
        await unFollow(id)
      }
      this.$toast('操作成功')
      this.detail.is_followed = !this.detail.is_followed
    },
    async like (id) {
      if (!this.$store.state.user.token.token) {
        this.$router.push({
          path: '/login',
          query: {
            goBack: 'aa'
          }
        })
        this.$toast('请先登录')
        return
      }
      if (this.detail.attitude === 1) {
        await escZan(id)
      } else {
        await goArticleZan(id)
      }
      this.detail.attitude = this.detail.attitude === 1 ? -1 : 1
    },
    async dislike (id) {
      if (!this.$store.state.user.token.token) {
        this.$router.push({
          path: '/login',
          query: {
            goBack: 'aa'
          }
        })
        this.$toast('请先登录')
        return
      }
      if (this.detail.attitude === 0) {
        await addDislike(id)
      } else {
        await unDislike(id)
      }
      this.$toast('操作成功')
      this.detail.attitude = this.detail.attitude === 0 ? -1 : 0
    }
  },
  components: {
    Comment
  }
}
</script>

<style lang='less' scoped>
.loading {
  height: 100vh;
  text-align: center;
  display: flex;
  align-items: center;
  justify-content: center;
}
.article-container{
  position: absolute;
  left: 0;
  top: 0;
  overflow-y: scroll;
  width: 100%;
  height: 100%;
}
.article-loading {
  padding-top: 100px;
  text-align: center;
}
.error{
  padding-top: 100px;
  text-align: center;
}
.detail {
  padding: 50px 10px;
  .title {
    font-size: 16px;
  }
  .zan{
    text-align: center;
  }
  .author {
    padding: 10px 0;
    display: flex;
    .text {
      flex: 1;
      padding-left: 10px;
      line-height: 1.3;
      .name {
        font-size: 14px;
        margin: 0;
      }
      .time {
        margin: 0;
        font-size: 12px;
        color: #999;
      }
    }
  }
  .content {
    font-size:14px;
    overflow: hidden;
    white-space: pre-wrap;
    word-break: break-all;
    ::v-deep img{
      max-width:100%;
      background: #f9f9f9;
    }
  }
}
</style>
